package jinyilw.swing.visuals.border;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.io.Serializable;

import javax.swing.UIManager;
import javax.swing.border.Border;

import jinyilw.swing.FontTools;
import jinyilw.swing.i18n.ICSetting;

public abstract class AbstractITitleBorder implements Border, Serializable
{
	private static final long serialVersionUID = -6624558663323731251L;

	protected Component ownerComponent;

	protected ICSetting i18nTitle;
	protected String title;
	protected int titlePosition;
	protected int titleJustification;
	protected Font titleFont;
	protected Color titleColor;

	static public final int DEFAULT_POSITION = 0;
	/**
	 * Position the title above the border's top line.
	 */
	static public final int ABOVE_TOP = 1;
	/**
	 * Position the title in the middle of the border's top line.
	 */
	static public final int TOP = 2;
	/**
	 * Position the title below the border's top line.
	 */
	static public final int BELOW_TOP = 3;
	/**
	 * Position the title above the border's bottom line.
	 */
	static public final int ABOVE_BOTTOM = 4;
	/**
	 * Position the title in the middle of the border's bottom line.
	 */
	static public final int BOTTOM = 5;
	/**
	 * Position the title below the border's bottom line.
	 */
	static public final int BELOW_BOTTOM = 6;

	/**
	 * Use the default justification for the title text.
	 */
	static public final int DEFAULT_JUSTIFICATION = 0;
	/**
	 * Position title text at the left side of the border line.
	 */
	static public final int LEFT = 1;
	/**
	 * Position title text in the center of the border line.
	 */
	static public final int CENTER = 2;
	/**
	 * Position title text at the right side of the border line.
	 */
	static public final int RIGHT = 3;
	/**
	 * Position title text at the left side of the border line for left to right
	 * orientation, at the right side of the border line for right to left
	 * orientation.
	 */
	static public final int LEADING = 4;
	/**
	 * Position title text at the right side of the border line for left to
	 * right orientation, at the left side of the border line for right to left
	 * orientation.
	 */
	static public final int TRAILING = 5;

	// Space between the border and the component's edge
	static protected final int EDGE_SPACING = 2;

	// Space between the border and text
	static protected final int TEXT_SPACING = 2;

	// Horizontal inset of text that is left or right justified
	static protected final int TEXT_INSET_H = 5;

	@Override
	public boolean isBorderOpaque()
	{
		return false;
	}

	public String getTitle()
	{
		if (i18nTitle != null)
			return i18nTitle.getI18nString();
		return title;
	}

	public ICSetting getI18nTitle()
	{
		return i18nTitle;
	}
	public int getTitlePosition()
	{
		return titlePosition;
	}
	public int getTitleJustification()
	{
		return titleJustification;
	}
	public Font getTitleFont()
	{
		if (titleFont == null)
			titleFont = UIManager.getFont("TitledBorder.font");
		if (i18nTitle != null && i18nTitle.isFontChange())
			return i18nTitle.getFont(titleFont);
		return titleFont;
	}
	public Color getTitleColor()
	{
		if (titleColor == null)
			titleColor = UIManager.getColor("TitledBorder.titleColor");
		return titleColor;
	}

	public void setTitle(String title)
	{
		this.title = title;
		if (ownerComponent != null && ownerComponent.isShowing())
			ownerComponent.repaint();
	}

	public void setI18nTitle(ICSetting i18nTitle)
	{
		this.i18nTitle = i18nTitle;
		if (ownerComponent != null && ownerComponent.isShowing())
			ownerComponent.repaint();
	}

	public void setTitlePosition(int titlePosition)
	{
		switch (titlePosition)
		{
			case ABOVE_TOP:
			case TOP:
			case BELOW_TOP:
			case ABOVE_BOTTOM:
			case BOTTOM:
			case BELOW_BOTTOM:
			case DEFAULT_POSITION:
				this.titlePosition = titlePosition;
				break;
			default:
				throw new IllegalArgumentException(
						titlePosition + " is not a valid title position.");
		}
		if (ownerComponent != null && ownerComponent.isShowing())
			ownerComponent.repaint();
	}

	public void setTitleJustification(int titleJustification)
	{
		switch (titleJustification)
		{
			case DEFAULT_JUSTIFICATION:
			case LEFT:
			case CENTER:
			case RIGHT:
			case LEADING:
			case TRAILING:
				this.titleJustification = titleJustification;
				break;
			default:
				throw new IllegalArgumentException(titleJustification
						+ " is not a valid title justification.");
		}
		if (ownerComponent != null && ownerComponent.isShowing())
			ownerComponent.repaint();
	}

	public void setTitleFont(Font font)
	{
		titleFont = font;
		if (ownerComponent != null && ownerComponent.isShowing())
			ownerComponent.repaint();
	}

	public void setTitleColor(Color color)
	{
		titleColor = color;
		if (ownerComponent != null && ownerComponent.isShowing())
			ownerComponent.repaint();
	}

	protected Font getFont(Component c)
	{
		Font font = getTitleFont();
		if (font != null)
			return font;
		//		if (c != null)
		//		{
		//			font = c.getFont();
		//			if (font != null)
		//				return font;
		//		}
		titleFont = FontTools.getCustomFont(13);
		return titleFont;
	}

	protected abstract Component getOwnerComponent();
}
